<?php
class Cache
{
	public static $dir;
	
	static public function init(){
		self::$dir = _cfg('path')."_inc/cache/";
	}
	
	static public function set($data = array(), $uuid = 0, $group = 'common', $time = 1){
		if(_cfg('cache')){
			self::init();
			$group = urlencode($group);
			$cache_arr = array();
			$time = time()+$time*60*60;
			$file = self::$dir.$group.".cache";
			if(file_exists($file))$cache_arr =  unserialize(file_get_contents($file));
			$cache_arr[$uuid] = array(
				"time"=>$time,
				"data"=>$data
			);
			$pull_data = serialize($cache_arr);
			return file_put_contents($file,$pull_data);
		}
	}
	
	static public function get($uuid = 0, $group = 'common'){
		if(_cfg('cache')){
			self::init();
			$group = urlencode($group);
			$file = self::$dir.$group.".cache";
			if(file_exists($file)){
				$cache_arr =  unserialize(file_get_contents($file));
				if(isset($cache_arr[$uuid]['time']) and time() < $cache_arr[$uuid]['time']){
					return $cache_arr[$uuid]['data'];
				}
				else{
					return false;
				}
			}
			else {
				return false;
			}
		}
	}
}